home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / utilsys / rss14gmd.lha / RSys_1.4gmd / C / Extern.c < prev    next >
C/C++ Source or Header  |  1996-05-04  |  9KB  |  421 lines

  1. /*
  2.    ***************************************************************************
  3.    *
  4.    * Datei:
  5.    *      RSysExtern.c
  6.    *
  7.    * Inhalt:
  8.    *
  9.    *      --- Globale Routinen ---
  10.    *
  11.    *    BPTR AskFileMode ( char *filename , WINDOW *wind );
  12.    *    int GetFile ( WINDOW *wind , char *dir , char *file , char *pattern , char *title , char *oktext );
  13.    *    int InitSpeech ( void );
  14.    *    int SysStarted ( int type );
  15.    *    void RemoveSpeech ( void );
  16.    *    void SaveList ( WINDOW *wind , char *filename , char *title , LIST *list , int withheader );
  17.    *    void Speak ( char *text );
  18.    *
  19.    *      --- Lokale  Routinen ---
  20.    *
  21.    *    static char *strip ( char *src , char *dest );
  22.    *
  23.    * Bemerkungen:
  24.    *      Verwaltung externer Daten und Schnittstellen zu Dateien.
  25.    *
  26.    * Erstellungsdatum:
  27.    *      07-Jul-93     Rolf Böhme
  28.    *
  29.    * Änderungen:
  30.    *      07-Jul-93     Rolf Böhme        Erstellung
  31.    *
  32.    ***************************************************************************
  33.  */
  34.  
  35. #include "RSys.h"
  36. #include "protos.h"
  37.  
  38.  
  39. static char *
  40. strip (char *src, char *dest)
  41. {
  42.   BYTE s = 0, d = 0, l = strlen (src);
  43.  
  44.   while (s < l)
  45.     {
  46.       if (!strchr ("'-:/ ", (int) src[s]))
  47.     dest[d++] = src[s++];
  48.       else
  49.     s++;
  50.     }
  51.  
  52.   dest[d] = STRINGEND;
  53.  
  54.   return dest;
  55. }
  56.  
  57.  
  58.  /*
  59.   * GetFile() bietet einen Filerequester zur Auswahl einer Datei
  60.   * an. Dabei wird unterschieden, ob die Datei zum Sichern oder zum
  61.   * Laden verwendet wird.
  62.   */
  63. int
  64. GetFile (WINDOW * wind, char *dir, char *file, char *pattern, char *title, char *oktext)
  65. {
  66.   FILEREQUESTER *AslFileRequest = NULL;
  67.   BYTE Result = FALSE;
  68.   APTR req = NULL;
  69.   UWORD l = 20, t = 20;
  70.   int mask = FILF_PATGAD;
  71.   char _oktext[MAXSHOW], _title[40], _pattern[10] = "#?", *_cancel = "Cancel";
  72.  
  73.   DPOS;
  74.  
  75.   PrintInfo ("Selecting file", NO_SPEAK, 0);
  76.  
  77.   if (Flags.speakmode)
  78.     Speak (title);
  79.  
  80.   if (AslBase = OpenLibrary ((UBYTE *) "asl.library", 37L))
  81.     {
  82.       if (wind)
  83.     {
  84.       req = LockWindow (wind);
  85.       CenterWindow (wind->WScreen, &t, &l, (UWORD) 260, (UWORD) 170);
  86.     }
  87.  
  88.       if (dir && *dir)
  89.     strncpy (_dir, dir, MAXFULLNAME);
  90.       else
  91.     strcpy (_dir, "RAM:");
  92.  
  93.       if (file && *file)
  94.     {
  95.       char header[PATHPARTWIDTH];
  96.  
  97.       strip (file, header);
  98.  
  99.       strncpy (_file, header, PATHPARTWIDTH);
  100.     }
  101.       else
  102.     strcpy (_file, "unknown");
  103.  
  104.       if (pattern && *pattern)
  105.     strncpy (_pattern, pattern, 10);
  106.       else
  107.     strcpy (_pattern, "#?");
  108.  
  109.       if (title && *title)
  110.     strncpy (_title, title, 40);
  111.       else
  112.     strcpy (_title, "Select file");
  113.  
  114.       if (oktext && *oktext)
  115.     strncpy (_oktext, oktext, MAXSHOW);
  116.       else
  117.     strcpy (_oktext, "Select");
  118.  
  119.       if (strstr (_oktext, "Save"))
  120.     mask |= FILF_SAVE;
  121.  
  122.       if (AslFileRequest = AllocAslRequestTags (ASL_FileRequest,
  123.                         ASL_Window, (ULONG) wind,
  124.                         ASL_Hail, (ULONG) _title,
  125.                         ASL_LeftEdge, (ULONG) l,
  126.                         ASL_TopEdge, (ULONG) t,
  127.                         ASL_OKText, (ULONG) _oktext,
  128.                         ASL_CancelText, (ULONG) _cancel,
  129.                         ASL_File, (ULONG) _file,
  130.                         ASL_Dir, (ULONG) _dir,
  131.                           ASL_Pattern, (ULONG) _pattern,
  132.                         ASL_FuncFlags, (ULONG) mask,
  133.                         TAG_DONE))
  134.     {
  135.       if (AslRequest (AslFileRequest, NULL))
  136.         {
  137.           if (AslFileRequest->rf_File)
  138.         {
  139.           strncpy (_fullpath, AslFileRequest->rf_Dir, MAXFULLNAME);
  140.           AddPart ((UBYTE *) _fullpath, (UBYTE *) AslFileRequest->rf_File, MAXFULLNAME);
  141. /*
  142.    if(AslFileRequest->rf_Dir)
  143.    {
  144.    strncpy(_fullpath,AslFileRequest->rf_Dir,MAXFULLNAME);
  145.    AddPart((UBYTE *)_fullpath,(UBYTE *)AslFileRequest->rf_File,
  146.    MAXFULLNAME);
  147.    }
  148.    else strncpy(_fullpath,AslFileRequest->rf_File,MAXFULLNAME);
  149.  */
  150.           Result = TRUE;
  151.         }
  152.  
  153.           if (AslFileRequest->rf_Pat)
  154.         strncpy (_pattern, AslFileRequest->rf_Pat, 10);
  155.         }
  156.  
  157.       FreeAslRequest (AslFileRequest);
  158.     }
  159.       else
  160.     ErrorHandle ("AllocAslRequest()", MEMORY_ERR, ALLOC_FAIL, NO_KILL);
  161.  
  162.       CloseLibrary (AslBase);
  163.  
  164.       UnlockWindow (req);
  165.     }
  166.   else
  167.     ErrorHandle ("asl.library", LIBRARY_ERR, OPEN_FAIL, NO_KILL);
  168.  
  169.   if (strcmp (_oktext, "Check"))
  170.     PrintStatistics ();
  171.  
  172.   return Result;
  173. }
  174.  
  175.  
  176. BPTR
  177. AskFileMode (char *filename, WINDOW * wind)
  178. {
  179.   int mode = OVERWRITE;
  180.   BPTR fout = NULL;
  181.  
  182.   if (exist (filename))
  183.     mode = MyEasyRequest (wind, (UBYTE *) NAME " ask you",
  184.               (UBYTE *) "Append|Overwrite|Cancel",
  185.               (UBYTE *) "The file <%s> exists!",
  186.               filename);
  187.  
  188.   switch (mode)
  189.     {
  190.     case APPEND:
  191.       fout = Open ((UBYTE *) filename, MODE_OLDFILE);
  192.       if (fout)
  193.     Seek (fout, 0L, OFFSET_END);
  194.       break;
  195.  
  196.     case OVERWRITE:
  197.       fout = Open ((UBYTE *) filename, MODE_NEWFILE);
  198.       break;
  199.     }
  200.  
  201.   if (mode && !fout)
  202.     ErrorHandle (filename, FILE_ERR, OPEN_FAIL, NO_KILL);
  203.  
  204.   return fout;
  205. }
  206.  
  207.  /*
  208.   * SaveList() speichert die Einträge einer Liste (vom Hauptfenster oder
  209.   * intern) in eine Datei ab oder direkt ins Clipboard.
  210.   */
  211. void
  212. SaveList (WINDOW * wind, char *filename, char *title,
  213.       LIST * list, int withheader)
  214. {
  215.   BPTR fout;
  216.   NODE *node;
  217.   char out[MAXWRITESIZE], fmt[MAXWRITESIZE];
  218.   int noheader;
  219.  
  220.   DPOS;
  221.  
  222.   PrintInfo ("Save list to file", NO_SPEAK, 0);
  223.  
  224.   fout = AskFileMode (filename, wind);
  225.  
  226.   if (fout)
  227.     {
  228.       noheader = RSysFormatOutput (wind, fmt);
  229.  
  230.       if (!noheader)
  231.     {
  232.       sprintf (out, "----------------------------------------------\n"
  233.            "%s\n"
  234.            "----------------------------------------------\n\n",
  235.            title);
  236.  
  237.       Write (fout, out, strlen (out));
  238.  
  239.       if (withheader)
  240.         {
  241.           sprintf (out, "%s\n----------------------------------------------\n",
  242.                EntryAttr[LastID].ea_header);
  243.           Write (fout, out, strlen (out));
  244.         }
  245.     }
  246.  
  247.       for (node = list->lh_Head; node->ln_Succ /*&& !IoErr() */ ;
  248.        node = node->ln_Succ)
  249.     {
  250.       sprintf (out, fmt, node->ln_Name);
  251.       strcat ((char *) out, "\n");
  252.  
  253.       Write (fout, out, strlen (out));
  254.     }
  255.  
  256.       if (IoErr ())
  257.     ErrorHandle (filename, FILE_ERR, WRITE_FAIL, NO_KILL);
  258.       else
  259.     {
  260.       Write (fout, (UBYTE *) "\n", 1);
  261.       PrintInfo ("List was written to file", SPEAK, SEC);
  262.     }
  263.  
  264.       Close (fout);
  265.     }
  266.  
  267.   PrintStatistics ();
  268.  
  269.   return;
  270. }
  271.  
  272.  /*
  273.   * SysStarted() überprüft, ob RSys ein zweites Mal gestartet wurde
  274.   * und sendet im zutreffenden Fall einen Argumenttyp der Art der
  275.   * anzuzeigenden Liste an den Task des zuerst gestarteten Programmes
  276.   */
  277. int
  278. SysStarted (int type)
  279. {
  280.   MSGPORT *TestPort;
  281.   MSGPORT *ReplyPort = NULL;
  282.   RSYS_SysMsg *mess;
  283.   int ret = FALSE;
  284.  
  285.   DPOS;
  286.  
  287.   ReplyPort = CreatePort ((UBYTE *) "RSys-Reply-Port", 0L);
  288.  
  289.   if (ReplyPort)
  290.     {
  291.       mess = (RSYS_SysMsg *) MyAllocVec (sizeof (RSYS_SysMsg), MEMF_CLEAR | MEMF_PUBLIC, NO_KILL);
  292.  
  293.       if (mess)
  294.     {
  295.       mess->sm_message.mn_Node.ln_Type = NT_MESSAGE;
  296.       mess->sm_message.mn_ReplyPort = ReplyPort;
  297.       mess->sm_message.mn_Length = sizeof (RSYS_SysMsg);
  298.       mess->sm_newtype = type;
  299.  
  300.       Forbid ();
  301.  
  302.       if (TestPort = FindPort ((UBYTE *) ID_PORT_NAME))
  303.         {
  304.           PutMsg (TestPort, (MESSAGE *) mess);
  305.  
  306.           Permit ();
  307.  
  308.           WaitPort (ReplyPort);
  309.  
  310.           ret = TRUE;
  311.         }
  312.       else
  313.         Permit ();
  314.  
  315.       MyFreeVec (mess);
  316.     }
  317.  
  318.       DeletePort (ReplyPort);
  319.     }
  320.  
  321.   return ret;
  322. }
  323.  
  324. LIBRARY *TranslatorBase = NULL;
  325. MSGPORT *narratorPort;
  326. NARRATOR_RB request;
  327.  
  328.  /*
  329.   * RemoveSpeech() entfernt die Ressourcen für die Sprachausgabe
  330.   * und schließt das Device, das für die Sprachausgabe des Amigas
  331.   * zuständig ist, das narrator.device
  332.   */
  333. void
  334. RemoveSpeech (void)
  335. {
  336.   DPOS;
  337.  
  338.   if (narratorPort)
  339.     DeleteMsgPort (narratorPort);
  340.   if (TranslatorBase)
  341.     CloseLibrary (TranslatorBase);
  342.   if (request.message.io_Device)
  343.     CloseDevice ((IOREQUEST *) & request);
  344.  
  345.   return;
  346. }
  347.  
  348.  /*
  349.   * InitSpeech() öffnet die Ressourcen für die Sprachausgabe
  350.   * Anm: Auf einem 4000er stürzt an dieser Stelle das Programm ab!
  351.   */
  352. int
  353. InitSpeech (void)
  354. {
  355.   DPOS;
  356.  
  357.   if (TranslatorBase = OpenLibrary ((UBYTE *) "translator.library", 0))
  358.     {
  359.       if (narratorPort = CreateMsgPort ())
  360.     {
  361.       if (NOT (OpenDevice ((UBYTE *) "narrator.device", 0, (IOREQUEST *) & request, 0)))
  362.         return (TRUE);
  363.       else
  364.         ErrorHandle ("narrator.device", DEVICE_ERR, OPEN_FAIL, NO_KILL);
  365.     }
  366.       else
  367.     ErrorHandle ("CreateMsgPort()", PORT_ERR, CREATE_FAIL, NO_KILL);
  368.     }
  369.   else
  370.     ErrorHandle ("translator.library", LIBRARY_ERR, OPEN_FAIL, NO_KILL);
  371.  
  372.   RemoveSpeech ();
  373.  
  374.   return FALSE;
  375. }
  376.  
  377.  /*
  378.   * Speak() gibt einen Text über das narrator.device aus
  379.   */
  380. void
  381. Speak (char *text)
  382. {
  383.   UBYTE help[BUFSIZE + 2];
  384.   UBYTE translate[3 * BUFSIZE];
  385.   UBYTE Channel[4] =
  386.   {1, 2, 4, 8};
  387.  
  388.   DPOS;
  389.  
  390.   if (!Flags.speakmode)
  391.  
  392.     strncpy ((char *) help, text, BUFSIZE);
  393.  
  394.   if (!ispunct ((int) text[strlen (text) - 1]))
  395.     strcat ((char *) help, ".");
  396.  
  397.   if (NOT (Translate (help, (strlen ((char *) help) * sizeof (char)), translate, sizeof (translate))))
  398.     {
  399.       request.message.io_Message.mn_ReplyPort = narratorPort;
  400.       request.message.io_Command = CMD_WRITE;
  401.       request.message.io_Data = (APTR) translate;
  402.       request.message.io_Length = sizeof (translate);
  403.  
  404.       request.rate = DEFRATE;    /* Worte pro Minute: 40 < rate < 400 */
  405.       request.pitch = DEFPITCH;    /* 65 < pitch < 320 */
  406.       request.sex = DEFSEX;    /* Geschlecht : auch MALE */
  407.       request.volume = DEFVOL;    /* Lautstaerke max 64 */
  408.       request.mode = DEFMODE;    /* Aussprache-Modus */
  409.       request.sampfreq = DEFFREQ;
  410.       request.F0enthusiasm = DEFF0ENTHUS;
  411.       request.F0perturb = DEFF0PERT;
  412.       request.priority = DEFPRIORITY;
  413.       request.ch_masks = (UBYTE *) Channel;    /* Kanaele */
  414.       request.nm_masks = sizeof (Channel);
  415.  
  416.       DoIO ((IOREQUEST *) & request);
  417.     }
  418.  
  419.   return;
  420. }
  421.